package com.paas.form.core.model.js;

import com.paas.form.core.model.ElementUI;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.Collections;

@EqualsAndHashCode(callSuper = true)
@Data
public class VueMethods extends ElementUI {

    //表单初始化方法
    public static final String FORM_INIT = "            /**\n" +
            "             * 初始化状态\n" +
            "             */\n" +
            "            init(parentResId, id,isdetail) {\n" +
            "                if(isdetail){\n" +
            "                    this.isdetail= true;\n" +
            "                }else {\n" +
            "                    this.isdetail= false;\n" +
            "                }" +
            "                this.addOrEdit = \"表单生成新增\";\n" +
            "                if (id) {\n" +
            "                    this.addOrEdit = \"表单生成编辑\";\n" +
            "                    let params = {\n" +
            "                        id: id,\n" +
            "                        status: \"1\",\n" +
            "                    };\n" +
            "                    ${className}Service.queryById(params).then(({data}) => {\n" +
            "                        if (data) {\n" +
            "                            delete data.json;\n" +
            "                            delete data.style;\n" +
            "                            let objData= {};\n" +
            "                            for(let key in data){\n" +
            "                                try {\n" +
            "                                    let toObj = JSON.parse(data[key]);\n" +
            "                                    if(toObj instanceof Object || toObj instanceof Array){\n" +
            "                                        objData[key] =toObj;\n" +
            "                                    }else {\n" +
            "                                        objData[key] =data[key];\n" +
            "                                    }\n" +
            "                                } catch {\n" +
            "                                    objData[key] =data[key];\n" +
            "                                }\n" +
            "                            }\n" +
            "                            this.${formDataModel.formModel} = objData;\n" +
            "                        }\n" +
            "                    });\n" +
            "                } else {\n" +
            "                    this.${formDataModel.formModel}.parentId = parentResId;\n" +
            "                }\n" +
            "                this.showDialog = true;\n" +
            "            },";

    public VueMethods(int indentNum) {
        this.indentNum = indentNum;
    }

    @Override
    public void getElHtml(StringBuilder str) {

    }

    @Override
    public void getStartTagElHtml(StringBuilder str) {
        String dataTemplate = String.join("", Collections.nCopies(indentNum, "\t")) + "methods: {";
        str.append(dataTemplate);
        str.append("\n");
        str.append(FORM_INIT);
        str.append("\n");

    }

    @Override
    public void getEndTagElHtml(StringBuilder str) {
        String returnTemplate = String.join("", Collections.nCopies(indentNum, "\t")) + "},";
        str.append(returnTemplate);
        str.append("\n");
    }
}